﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace UniverDataServiceLib
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class UniverDataService : IUniverDataService
    {
        SqlConnection connection;

        public UniverDataService()
        {
            connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString);
        }

        /// <summary>
        /// Gets data for any university object in DB
        /// </summary>
        /// <param name="uniqueNo"></param>
        /// <returns></returns>
        public UObjectFullData GetObjectData(int uniqueNo)
        {
            var data = new UObjectFullData();
            
            connection.Open();

            // get current object's type name
            SqlCommand command = new SqlCommand("GetClassName", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@uniqueNo", uniqueNo));

            SqlDataReader reader = command.ExecuteReader();
            
            if (reader.Read())
            {
                data.ClassName = reader.GetString(0);
            }
            reader.Close();

            // get object properties and their values
            command.CommandText = "GetObjectData";
            command.Parameters.Clear();
            command.Parameters.Add(new SqlParameter("@unique_num", uniqueNo));

            reader = command.ExecuteReader();
            var fields = new List<UObjectPropertie>();
           
            while (reader.Read())
            {
                fields.Add(new UObjectPropertie()
                {
                    ClassNum = reader.GetInt32(0),
                    FieldName = reader.GetString(1),
                    FieldType = reader.GetInt32(2),
                    FieldData = reader.GetString(3)
                });
            }

            data.Fields = fields.ToArray();
            connection.Close();

            return data;
        }

        /// <summary>
        /// Gets all child nodes for some parent node
        /// </summary>
        /// <param name="parentId">Id of parent node</param>
        /// <returns></returns>
        public UObject[] GetChildUObjects(int parentId)
        {
            List<UObject> retVal = new List<UObject>();

            connection.Open();

            SqlCommand command = new SqlCommand("GetChildren", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@parent_num", parentId));

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                int major = (int)reader["Major"];

                if (parentId == major)
                {
                    retVal.Add(new UObject() { Name = reader["Name"].ToString(), UniqueNo = (int)reader["UniqueNo"] });
                }
            }

            connection.Close();    
            return retVal.ToArray();            
        }

        /// <summary>
        /// Test method
        /// </summary>
        /// <returns></returns>
        public UObjectPropertie Get1()
        {
            return new UObjectPropertie() { ClassNum = 10 };
        }
    }
}
